/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gujproject;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author achal
 */
public class CorpusClass {

    public CorpusClass()
            {
        return;
    }

    public int makeSingleFile()
    {
        File rootDir=new File("/home/achal/Desktop/gujcorp");
        File [] secondDir;
        File [] thirdDir;
        File [] corpus;
        String sampleString;
        File outputFile=new File("corpus.txt");

        String strLine=new String();
        String [] temp=null;

        int flag;

        BufferedWriter obuff;
        Pattern p=Pattern.compile("[\u0A80-\u0AFF]+");


        try{
            obuff=new BufferedWriter(new FileWriter(outputFile));
        }catch(IOException e){
            e.printStackTrace();
            return 0;
        }


        if(rootDir.isDirectory())
        {
            secondDir=rootDir.listFiles();
            System.out.println(secondDir.length);
            for(int i=0;i<secondDir.length;i++)
            {

                if(secondDir[i]==null || secondDir[i].isDirectory()==false)
                {
                    continue;
                }

                thirdDir=secondDir[i].listFiles();
                if(thirdDir[0]==null || thirdDir[0].isDirectory()==false)
                {
                    continue;
                }

                corpus=thirdDir[0].listFiles();
                try
                {

                            System.out.println(thirdDir[0]+" "+i);




                    if(corpus[0]!=null)
                    {
                    BufferedReader ibuff=new BufferedReader(new FileReader(corpus[0]));

                        while((strLine=ibuff.readLine())!=null)
                        {

                            temp=strLine.split(" ");
                            flag=0;
                            for(int j=0;j<temp.length;j++)
                            {

                                Matcher m=p.matcher(temp[j]);
                                Boolean b=m.find();

                                if(b==true)
                                {
                                    flag=1;
                                    obuff.write(m.group()+" ");
                                }
                            }
                            if(flag==1)
                            {
                                obuff.write("\n");
                            }
                        }

                    }
                }catch(IOException ex){
                    ex.printStackTrace();
                }catch(NullPointerException e){
                    e.printStackTrace();
                }
            }
        }
        return 1;
    }

    public int makeLexicon()
    {
        File input=new File("corpus.txt");
        File output=new File("lexicon.txt");
        String line=new String();
        String [] words=null;
        int i;
        int value;
        String key=new String();

        Map<String, Integer> Lexicon=new HashMap<String, Integer>();

        try
        {
            BufferedReader ibuff=new BufferedReader(new FileReader(input));

            while((line=ibuff.readLine())!=null)
            {
                words=line.split(" ");

                for(i=0;i<words.length;i++)
                {
                    if(Lexicon.containsKey(words[i]))
                    {
                        value=(int)Lexicon.get(words[i]);
                        value++;
                        Lexicon.remove(words[i]);
                        Lexicon.put(words[i], value);
                    }
                    else
                    {
                        Lexicon.put(words[i], 1);
                    }
                }
            }
            
            BufferedWriter obuff=new BufferedWriter(new FileWriter(output));

            Set keySet=Lexicon.keySet();

            Iterator iter=keySet.iterator();

            while(iter.hasNext())
            {
                key=(String)iter.next();
                value=Lexicon.get(key);
                obuff.write(key+" "+value+"\n");
            }

        }catch(FileNotFoundException e)
        {
            e.printStackTrace();
            return 0;
        }catch(IOException e)
        {
            e.printStackTrace();
            return 0;
        }

        return 1;
    }
    public int augmentLexicon()
    {
        File lexicon=new File("lexicon.txt");
        File root=new File("../../datasets/refilter");
        File []newFiles;
        String line=new String();
        String []words=null;
        String key=new String();
        int i, j;
        int value;
        Pattern p=Pattern.compile("[\u0A80-\u0AFF]");
        Matcher m;

        Map<String, Integer> Lexicon=new HashMap<String, Integer>();

        if(root.isDirectory()!=true)
        {
            System.out.println("Incorrect files structure");
            return 0;
        }

        newFiles=root.listFiles();
        try
        {
            BufferedReader ibuff=new BufferedReader(new FileReader(lexicon));
            while((line=ibuff.readLine())!=null)
            {
                words=line.split(" ");
                for(i=0;i<words.length-1;i++)
                {
                    value=Integer.parseInt(words[i+1]);
                    Lexicon.put(words[i], value);
                }
            }


            for(i=0;i<newFiles.length;i++)
            {
                ibuff=new BufferedReader(new FileReader(newFiles[i]));
                while((line=ibuff.readLine())!=null)
                {
                    //words=line.split(" ");
                    m=p.matcher(line);
                    if(line==null)
                    {
                        continue;
                    }
                    
                    if(m.matches())
                    {
                        if(Lexicon.get(line)==null)
                        {
                            value=0;
                        }else
                        {
                            value=Lexicon.get(line);
                        }
                        value++;
                        Lexicon.put(line, value);
                    }
                }
            }

            ibuff.close();

            BufferedWriter obuff=new BufferedWriter(new FileWriter(lexicon));

            Set keySet=Lexicon.keySet();

            Iterator iter=keySet.iterator();

            while(iter.hasNext())
            {
                key=(String)iter.next();
                value=Lexicon.get(key);
                obuff.write(key+" "+value+"\n");
            }

        }catch(FileNotFoundException ex)
        {
            ex.printStackTrace();
        }catch(IOException ex)
        {
            ex.printStackTrace();
        }

        return 1;
    }
}
